package com.codeiy.util;

import cn.hutool.core.collection.CollectionUtil;
import com.codeiy.constant.CommonConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.List;

@Component
@RequiredArgsConstructor
public class DevPlatformUtils {
    @Value("${micro-service.dev-platform.service-name}")
    private String devServiceName;
    private final DiscoveryClient discoveryClient;
    private final WebClient.Builder webClientBuilder;

    public boolean isAlive() {
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(devServiceName);
        return CollectionUtil.isNotEmpty(serviceInstanceList);
    }

    public WebClient create(String path) {
        return webClientBuilder.baseUrl("http://" + devServiceName + path).build();
    }
}
